<?php

class IndexController {

    public $page_title;
/* ------------------------------------------------------------------------------------------------- */


    public function login() {
        include '../icloud_files/models/UserModel.php';
        $user = new UserModel();
        $user->page_title = 'Login';

        if(isset($_POST['email']) && isset($_POST['sanity_check_base']) && isset($_POST['sanity_check'])) {
            $user->reset_password($_POST['email'], $_POST['sanity_check_base'], $_POST['sanity_check']);

        } else if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['pass'])) {
            $user->login_user($_POST['email'], $_POST['pass']);
            if(count($user->messages) == 0 && count($user->user_data) > 0) {
                $_SESSION['is_logged'] = true;
                $_SESSION['user_data'] = $user->user_data;
                $_SESSION['user_data']['currency'] = '&#8358;';
                $_SESSION['user_data']['currency_rate'] = $user->get_currency_rate($_SESSION['user_data']['currency']);

                if($_SESSION['user_data']['account_type'] === 'admin') {
                    header('Location: ?p=admin');
                    exit;
                } else {
                    header('Location: ?');
                    exit;
                }
            }
        }

        View::get_instance()->render('login', $user);
    }
/* ------------------------------------------------------------------------------------------------- */


    public function about() {
        include '../icloud_files/models/UserModel.php';
        $user = new UserModel();
        $user->page_title = 'About Us';

        if($_SERVER['REQUEST_METHOD'] == 'POST') {
            $user->contact_form($_POST);
        }

        $user->get_states();

        View::get_instance()->render('about', $user);
    }
/* ------------------------------------------------------------------------------------------------- */


    public function dealer_search() {
        include '../icloud_files/models/UserModel.php';
        $user = new UserModel();
        $user->page_title = 'Auto Service Center';

        $user->get_states();
        $user->list_dealers($_GET);

        View::get_instance()->render('dealer_search', $user);
    }
/* ------------------------------------------------------------------------------------------------- */


    public function logout() {
        session_destroy();
        header('Location: ?');
    }
/* ------------------------------------------------------------------------------------------------- */

}